﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DNA.Models;

namespace DNA.Controllers
{
    public class ReportController : Controller
    {
        private DNAAnalysisEntities db = new DNAAnalysisEntities();

        //
        // GET: /Report/

        public ViewResult Report()
        {
            // IList<Research> researches = db.Research.ToList<Research>();
            IList<Disease> diseases = db.Disease.ToList<Disease>();
            Dictionary<string, double> dnaReportDataPartial = null;

            #region busca por pesquisa
            //if(researches != null && researches.Count != 0)
            //{
            //    dnaReportData = new Dictionary<string, string>();

            //    foreach(Research research in researches)
            //    {
            //        if(userDNA.CompleteDNASequence.Contains(research.DNAPartialPattern))
            //        {
            //            ResearchDiseaseProbability diseaseResearch = db.ResearchDiseaseProbability.First(r => r.Research_Id == research.Id);
            //            string diseaseName = db.Disease.First(d => d.Id == diseaseResearch.Disease_Id).Name;
            //            dnaReportData.Add(diseaseName, diseaseResearch.Probability);
            //        }
            //    }
            //}
            #endregion

            if(diseases != null && diseases.Count != 0)
            {
                int userId = int.Parse(Session["UserId"].ToString());
                string userDNA = pegaDNAUsuarioLogado();
                dnaReportDataPartial = new Dictionary<string, double>();
                foreach(Disease disease in diseases)
                {
                    ResearchDiseaseProbability rdp = db.ResearchDiseaseProbability.First(rd => rd.Disease_Id == disease.Id);
                    Research research = db.Research.First(r => r.Id == rdp.Research_Id);
                    if(dnaReportDataPartial.Count > 0)
                    {
                        foreach(var item in dnaReportDataPartial)
                        {
                            if(item.Key == disease.Name)
                            {
                                double oldProbability = double.Parse(item.Value.ToString().Substring(0,5));
                                dnaReportDataPartial.Remove(item.Key);
                                dnaReportDataPartial.Add(disease.Name, Math.Round((oldProbability * double.Parse(rdp.Probability.ToString().Substring(0,5))),2));
                            }
                        }
                    }                    
                    if(userDNA.Contains(research.DNAPartialPattern))
                    {
                        dnaReportDataPartial.Add(disease.Name, Math.Round(rdp.Probability, 2));
                    }
                    else
                    {
                        dnaReportDataPartial.Add(disease.Name, -1);
                    }
                }                
            }
            Dictionary<string, string> dnaReportData = new Dictionary<string, string>();
            foreach(var item in dnaReportDataPartial)
            {
                if(item.Value > -1)
                {
                    dnaReportData.Add(item.Key, item.Value.ToString() + "%");
                }
                else
                {
                    dnaReportData.Add(item.Key, "N/A");
                }
            }
            return View(dnaReportData);
        }

        private string pegaDNAUsuarioLogado()
        {
            int userId = Convert.ToInt32(Session["UserId"].ToString());
            User user = db.User.Single(u => u.Id == userId);

            DNA.Models.DNA dna = db.DNA.Single(d => d.User.Id == user.Id);
            string arquivo = dna.CompleteDNASequence;

            string filePath = Server.MapPath("~/DNAFiles/") + arquivo;
            string line;
            string DnaFull = "";

            using(StreamReader reader = new StreamReader(filePath))
            {
                while((line = reader.ReadLine()) != null)
                {
                    DnaFull = DnaFull + line;
                }
            }

            return DnaFull;

        }

        //
        // GET: /Report/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Report/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Report/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Report/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Report/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Report/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Report/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
